package com.wenwei.activity;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wenwei.adapter.BalanceAdapter;
import com.wenwei.bean.UserInfoBean;
import com.wenwei.bean.WalletHisBean;
import com.wenwei.net.ApiManager;
import com.wenwei.net.BaseSubscriber;
import com.wenwei.parking.BaseActivity;
import com.wenwei.parking.Constant;
import com.wenwei.parking.R;
import com.wenwei.utils.RogerSPUtils;
import com.wenwei.utils.UserStringUtils;
import com.wenwei.view.FullyLinearManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/8/21.
 */
public class BalanceActivity extends BaseActivity {
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.recycler)
    XRecyclerView recycler;
    @Bind(R.id.balance_tv)
    TextView balanceTv;

    BalanceAdapter mAdapter;
    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_balance;
    }

    @Override
    protected void initView() {
        super.initView();

        UserInfoBean ub = RogerSPUtils.getObject(mContext, Constant.SP_USER_USER_RESULT, UserInfoBean.class);
        if (ub != null) {
            balanceTv.setText(""+ub.getBalance());
        }

        baseTitle.setText("余额");
        recycler.setHasFixedSize(true);
        recycler.setLayoutManager(new FullyLinearManager(mContext));
        recycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
            }

            @Override
            public void onLoadMore() {
                if (currentPage < pageTotalCount) {
                    currentPage = currentPage + 1;
                    recycler.setLoadingMoreEnabled(true);
                    getWalletLog();
                } else {
                    recycler.setLoadingMoreEnabled(false);
                    recycler.loadMoreComplete();
                }
            }
        });
        recycler.setPullRefreshEnabled(false);
        mAdapter = new BalanceAdapter();
        recycler.setAdapter(mAdapter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.base_back, R.id.btn_chong_zhi})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finish();
                break;
            case R.id.btn_chong_zhi:
                goActivity(mContext,RechargeActivity.class);
                break;
        }
    }


    @Override
    protected void initData() {
        super.initData();
        getWalletLog();
    }

    private int currentPage = 1;//当前分页
    private int pageTotalCount;//分页总数
    private BaseSubscriber<WalletHisBean> walletSb;
    private List<WalletHisBean.DataBean> mList = new ArrayList<>();
    private void getWalletLog(){
        walletSb = new BaseSubscriber<WalletHisBean>(mContext) {
            @Override
            public void onSuccess(WalletHisBean walletHisBean) {

                pageTotalCount = walletHisBean.getTotal_page();

                mList.addAll(walletHisBean.getData());
                mAdapter.setmList(mList);
                recycler.loadMoreComplete();
            }
        };
        ApiManager.getInstance().getWalletLog(walletSb, UserStringUtils.getUserId(mContext),currentPage);
    }
}
